package top.guxx.mall.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.guxx.mall.config.RedisService;
import top.guxx.mall.constants.Constants;
import top.guxx.mall.dao.IProductDao;
import top.guxx.mall.domain.dto.CartDTO;
import top.guxx.mall.domain.dto.ProductDTO;
import top.guxx.mall.domain.po.Product;
import top.guxx.mall.domain.vo.ProductVO;
import top.guxx.mall.service.IProductService;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author：Guxx
 * @Description：
 * @Date：2025/2/23 4:00
 * @Filename：ProductService
 */
@Service
@Slf4j
public class ProductService implements IProductService {
    @Resource
    private IProductDao productDao;

    @Resource
    private RedisService redisService;

    @Override
    public List<ProductVO> queryProductByProductIds(List<String> productIds) {
        return productDao.queryProductByProductIds(productIds);
    }

    @Override
    public List<ProductDTO> queryProduct(String userId) {
        List<String> favoriteList = queryFavoriteList(userId);
        List<String> cartList = queryCartList(userId);
        List<Product> products = productDao.queryAll();
        ArrayList<ProductDTO> res = new ArrayList<>();
        for (Product product : products) {
            ProductDTO dto = new ProductDTO();
            BeanUtils.copyProperties(product,dto);
            dto.setIsFavorite(favoriteList.contains(product.getProductId()));
            dto.setIsInCart(cartList.contains(product.getProductId()));
            res.add(dto);
        }
        return res;
    }

    @Override
    public List<CartDTO> queryInCart(String userId) {
        List<String> list = redisService.getAllElements(Constants.RedisKey.CART.getCode() + userId);
        List<String> ids = new ArrayList<>(new HashSet<>(list));
        List<CartDTO> res = new ArrayList<>();

        Map<String, Integer> map = new HashMap<>();
        for (String id : list) {
            map.put(id, map.getOrDefault(id, 0) + 1);
        }

        if(ids.isEmpty()){
            return res;
        }
        List<ProductVO> productVOS = productDao.queryProductByProductIds(ids);
        for (ProductVO productVO : productVOS) {
            res.add(CartDTO.builder()
                    .productId(productVO.getProductId())
                    .productName(productVO.getProductName())
                    .price(productVO.getPrice())
                    .picUrl(productVO.getUrl())
                    .nums(map.get(productVO.getProductId()))
                    .build());
        }
        return res;
    }

    @Override
    public List<CartDTO> queryFavorite(String userId) {
        List<String> list = redisService.getAllElements(Constants.RedisKey.FAVORITE.getCode() + userId);
        List<String> ids = new ArrayList<>(new HashSet<>(list));
        List<CartDTO> res = new ArrayList<>();

        Map<String, Integer> map = new HashMap<>();
        for (String id : list) {
            map.put(id, map.getOrDefault(id, 0) + 1);
        }

        if(ids.isEmpty()){
            return res;
        }
        List<ProductVO> productVOS = productDao.queryProductByProductIds(ids);
        for (ProductVO productVO : productVOS) {
            res.add(CartDTO.builder()
                    .productId(productVO.getProductId())
                    .productName(productVO.getProductName())
                    .price(productVO.getPrice())
                    .picUrl(productVO.getUrl())
                    .nums(map.get(productVO.getProductId()))
                    .build());
        }
        return res;
    }

    @Override
    public void add2Cart(String userId, String productId) {
        redisService.rightPush(Constants.RedisKey.CART.getCode() + userId, productId);
    }

    @Override
    public void add2Favorite(String userId, String productId) {
        redisService.rightPush(Constants.RedisKey.FAVORITE.getCode() + userId, productId);
    }

    private List<String> queryCartList(String userId){
        List<String> list = redisService.getAllElements(Constants.RedisKey.CART.getCode() + userId);
        return new ArrayList<>(new HashSet<>(list));
    }

    public List<String> queryFavoriteList(String userId) {
        List<String> list = redisService.getAllElements(Constants.RedisKey.FAVORITE.getCode() + userId);
        return new ArrayList<>(new HashSet<>(list));
    }
}
